<?php

namespace app\home\controller;

use app\admin\model\company\Example;
use app\admin\model\company\Product;
use app\admin\model\company\Setting;
use app\admin\model\company\Share;
use app\admin\model\company\Testimonial;
use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $currentUrl = request()->url();
        $this->assign('currentUrl', $currentUrl);

        //行业成熟应用
        $productIds = Setting::getValueByKey('homepage_recommend_product');
        $productIds = explode(',', $productIds);
        $centerProductId = reset($productIds);

        $centerProduct = Product::get($centerProductId);
        $otherProducts = Product::all(function ($query) use ($centerProductId) {
            return $query->where('id', 'neq', $centerProductId);
        });
        $this->assign('centerProduct', $centerProduct);
        $this->assign('otherProducts', $otherProducts);

        //运营分享
        $shareVideoCount = count(Share::all(function ($query) {
            return $query->where('share_type_id', 1); // 1=视频
        }));
        $shareArticleCount = count(Share::all(function ($query) {
            return $query->where('share_type_id', 2); // 2=文章
        }));
        //从配置中得到
//        $shareIds = Setting::getValueByKey('homepage_share');
//        $shareIds = explode(',', $shareIds);
//        $shareIds = array_slice($shareIds, 0, 5);
//
//        $shares = Share::all($shareIds);

        $shareModel = new Share();
        $shares = $shareModel->order('id', 'desc')->limit(5)->select();

        $this->assign('shareVideoCount', $shareVideoCount);
        $this->assign('shareArticleCount', $shareArticleCount);
        $this->assign('shares', $shares);

        //首页案例
        $pcExampleIds = Setting::getValueByKey('homepage_pc_example');
        $pxExamples = Example::all($pcExampleIds);

        $miniappExampleIds = Setting::getValueByKey('homepage_miniapp_example');
        $miniappExamples = Example::all($miniappExampleIds);

        $this->assign('pcExamples', $pxExamples);
        $this->assign('miniappExamples', $miniappExamples);

        //客户怎么说
        $testimonials = Testimonial::all();
        $this->assign('testimonials', $testimonials);



        return $this->view->fetch();
    }

}
